class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true
  belongs_to :user
  
  validates_presence_of :content
  validates_presence_of :author_email, :author, :unless => :user_id
  validates_format_of :author_email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :on => :create, :alert => "is invalid", :unless => :user_id

  #comments can be approved (set to true), not approved (set to false) and awaiting approval (nil)
  scope :approved, where(:approved => true)
  scope :awaiting_approval, where(:approved => nil)
  scope :not_approved, where(:approved => false)

  before_save :comment_approval
  
  def comment_approval
    if user_id
      self.approved = true
    end
  end

  #db is prepared for has_ancestry although Comment is not fully prepared to use it
  #has_ancestry
end

